<?php

namespace IDock\Insurance\AMIC;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Common\Tools\XMLTools;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Insurance\AMIC\Request\AllAMICRequest;
use IDock\Insurance\AMIC\Response\AllAMICResponse;

class Builder implements BuilderInterface
{

    public function getConfig(): array
    {
        return  $this->config;
    }

    protected $ip;

    protected $config;

    protected $appId;

    protected $appKey;
    protected $channelName;

    public function execute(Request $request): Response
    {
        /*** @var AllAMICRequest $request */
        $request->loadConfig($this->config);
        $request->setRequestId(md5(uniqid()));
        $request->setHeaders(['Content-Type' => 'application/xml']);
        $data = $this->generate($request);
        $header = [
            'requestType' => $request->requestType,
            'appId' => $this->appId,
            'timeStamp' => date('YmdHis'),
        ];
        $param = [
            'Head' => $header,
            'Body' => $data,
        ];
        $request->setPreParameter($param);
        $request = $this->sign($request);
        $xml = XMLTools::ToXML($request->getPreParameter(),'Packet','<?xml version="1.0" encoding="UTF-8"?>');
        $xml = mb_convert_encoding($xml,'GBK','UTF-8');
        $request->setBizParam($xml);
        if($request->requestType == 'N0218'){
            $request->setHeaders(['Content-Type' => 'application/json']);
            $paramJson = [
                'head' => $param['Head'],
                'baseInfo' => $param['Body'],
            ];
            $request->setPreParameter($paramJson);
            $request->setBizParam(json_encode($paramJson));
        }
        return $this->request($request);
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }

    public function sign(Request $request): Request
    {
        $param = $request->getPreParameter();
        $body = $param['Body'] ?? [];
        $header = $param['Head'] ?? [];
        $stringA = $this->appId.($header['timeStamp' ?? '']).$request->requestType;
        $stringA = substr($stringA,1,strlen($stringA) - 2);
        $signString = $stringA.'&'.$this->appKey.'&'.$this->channelName;
        $param['Head']['sign'] = md5($signString);
        $request->setPreParameter($param);
        return $request;
    }


    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request): Response
    {
        /**
         * @var AllAMICResponse $response
         */
        $response = $request->request($request);
        $response->setVisitor($request->getVisitor());
        if($response->isSuccess()){
            $response->printerInit();
        }
        return $response;
    }

    /**
     * 数据加密
     * @param Request $request
     */
    public function encrypt(Request $request)
    {

    }
}


